#include "CBaseObject.h"

BaseObject::BaseObject(void)
{
//    m_v3Direction = Ogre::Vector3(0,0,0);
//    m_v3Position = Ogre::Vector3(0,0,0);
    m_sceneNode = NULL;
    m_movableObject = NULL;
    m_Transform = btTransform(btQuaternion(0,0,0,1),btVector3(0,0,0));
    m_propertyMap.clear();
    m_Type = TYPE_NONE;
    m_collisionShape = NULL;
    m_rigidBody = NULL;
    m_controlable = false;
    m_isPhysicsRelevant = false;
    m_isGraphicsRelevant = false;
}

BaseObject::~BaseObject(void)
{
}

void BaseObject::setPosition(float fX, float fY, float fZ)
{
        if(m_sceneNode != NULL)
	{
            m_sceneNode->setPosition(Ogre::Real(fX), Ogre::Real(fY), Ogre::Real(fZ));
	}

        m_Transform.setOrigin(btVector3(fX,fY,fZ));
}

void BaseObject::setDirection(float fX, float fY, float fZ)
{
        if(m_sceneNode == NULL)
	{
		writeDebugOutput("CBaseObject::setDirection(): SceneNode is NULL!. Aborting!");
                return;
	}

        m_sceneNode->setDirection(Ogre::Real(fX), Ogre::Real(fY), Ogre::Real(fZ), Ogre::Node::TS_WORLD);
}

void BaseObject::getPosition(float *pfX, float *pfY, float *pfZ)
{
	if((pfX == NULL)||(pfX == NULL)||(pfX == NULL))
	{
		writeDebugOutput("CBaseObject::getPosition(): Some pointers are not allocated! Aborting!");
                return ;
	}

        *pfX = m_Transform.getOrigin().getX();
        *pfY = m_Transform.getOrigin().getY();
        *pfZ = m_Transform.getOrigin().getZ();
}

void BaseObject::getDirection(float *pfX, float *pfY, float *pfZ)
{
	// TODO: implement getDirection
        writeExceptionOutput("CBaseObject::getDirection(): not yet implemented!");
}

Ogre::SceneNode *BaseObject::getSceneNode()
{
        if(m_sceneNode == NULL)
	{
		writeDebugOutput("CBaseObject::getSceneNode(): SceneNode is NULL!. Aborting!");
                return NULL;
	}

        return m_sceneNode;
}

void BaseObject::setSceneNode(Ogre::SceneNode* SceneNode)
{
        m_sceneNode = SceneNode;
        m_sceneNode->setPosition(m_Transform.getOrigin().getX(),
                                       m_Transform.getOrigin().getY(),
                                       m_Transform.getOrigin().getZ()
                                       );
        m_sceneNode->setOrientation(m_Transform.getRotation().getW(),
                                          m_Transform.getRotation().getX(),
                                          m_Transform.getRotation().getY(),
                                          m_Transform.getRotation().getZ()
                                          );

}

void BaseObject::movableObject(Ogre::MovableObject *object)
{
    m_movableObject = object;
}

Ogre::MovableObject *BaseObject::movableObject()
{
    return m_movableObject;
}

//Ogre::Vector3 *CBaseObject::getv3Position()
//{
//	return &m_v3Position;
//}

//Ogre::Vector3 *CBaseObject::getv3Direction()
//{
//	return &m_v3Direction;
//}

void BaseObject::getWorldTransform(btTransform &worldTrans) const
{
        worldTrans = m_Transform;
}

void BaseObject::setWorldTransform(const btTransform &worldTrans)
{
        m_Transform = worldTrans;
        btQuaternion rot = worldTrans.getRotation();
        m_sceneNode->setOrientation(rot.w(), rot.x(), rot.y(), rot.z());
        btVector3 pos = worldTrans.getOrigin();
        m_sceneNode->setPosition(pos.x(), pos.y(), pos.z());
}

void BaseObject::setProperty(std::string propertyName, std::string propertyValue)
{
    m_propertyMap[propertyName] = propertyValue;
}

std::string BaseObject::getProperty(std::string propertyName)
{
    return m_propertyMap[propertyName];
}

void BaseObject::setType(BaseObject::Type type)
{
    m_Type = type;
}

BaseObject::Type BaseObject::getType()
{
    return m_Type;
}

void BaseObject::setCollisionShape(btCollisionShape *collisionShape)
{
    m_collisionShape = collisionShape;
}

btCollisionShape *BaseObject::getCollisionShape()
{
    return m_collisionShape;
}

void BaseObject::setRigidBody(btRigidBody *rigidBody)
{
    m_rigidBody = rigidBody;
}

btRigidBody *BaseObject::getRigidBody()
{
    return m_rigidBody;
}

bool BaseObject::isControlable()
{
    return m_controlable;
}

void BaseObject::setControlable(bool isControlable)
{
    m_controlable = isControlable;
}

bool BaseObject::isPhysicsRelevant()
{
    return m_isPhysicsRelevant;
}

void BaseObject::isPhysicsRelevant(bool isRelevant)
{
    m_isPhysicsRelevant = isRelevant;
}

bool BaseObject::isGraphicsRelevant()
{
    return m_isGraphicsRelevant;
}

void BaseObject::isGraphicsRelevant(bool isRelevant)
{
    m_isGraphicsRelevant = isRelevant;
}
